package com.kai.controller;

import com.kai.pojo.Emp;
import com.kai.pojo.EmpQueryParam;
import com.kai.pojo.PageResult;
import com.kai.pojo.Result;
import com.kai.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;



@RequestMapping("/emps")
@Slf4j
@RestController
public class EmpController {

    //DI注入
    @Autowired
    private EmpService empService;

    //分页查询
    @GetMapping
    public Result page(EmpQueryParam empQueryParam) {
        log.info("分页查询接收参数>: {}", empQueryParam);
        PageResult<Emp> pageResult = empService.page(empQueryParam);
        return Result.success(pageResult);
    }

    //添加员工
    @PostMapping
    public Result add(@RequestBody Emp emp) {
        log.info("添加员工>: {}", emp);
        empService.add(emp);
        return Result.success();
    }

    //批量|单独删除员工
    @DeleteMapping
    public Result deleteByIds(@RequestParam List<Integer> ids){

        log.info("删除员工>: {}", ids);
        empService.deleteByIds(ids);
        return Result.success();
    }

    //查询回显
    @GetMapping("/{id}")
    public Result getEmpInfoById(@PathVariable Integer id){

        log.info("查询回显>: {}", id);
        Emp emp =  empService.getEmpInfoById(id);
        return Result.success(emp);
    }

    //修改员工
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("修改员工>: {}", emp);
        empService.update(emp);
        return Result.success();
    }

}